package com.batata.continuing.web.demo;

import com.batata.continuing.config.moreNote.token.NotNeedToken;
import com.batata.continuing.service.billing.BillingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@AllArgsConstructor
@Api(tags = "Redisson测试使用")
@RequestMapping("/testRedisson")
public class TestRedissonController {

    @Resource
    private final RedissonClient redissonClient;

//    @Resource
//    private final BillingService billingService;

    @NotNeedToken
    @ApiOperation("testRedisson")
    @GetMapping("/testRedisson001")
    public String testRedisson001() throws InterruptedException {

        RLock lock = redissonClient.getLock("anyLock");

        // 最大等待时间，自动释放时间
        boolean b = lock.tryLock(1, 15, TimeUnit.SECONDS);

        if (b) {
            try {
                System.out.println("执行业务 开始");
                Thread.sleep(10000);
                System.out.println("执行业务 结束");
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        } else {
            return "获取锁失败";
        }


        return "执行结束";
    }


}
